﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Controls;

namespace LotteryVote
{
    public partial class WinLostReport : AgentBasePage
    {
        
        User user;
        IReportControl report;
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                InitParams();
            });
        }

        void InitParams()
        {
            int userId;
            if (string.IsNullOrEmpty(Request["UserId"]) || !int.TryParse(Request["UserId"], out userId))
                user = CurrentUser;
            else
            {
                UserFacade userFacade = new UserFacade();
                user = userFacade.GetUser(userId);
            }

            if (user.Role == SystemRole.Company)
            {
                var reportControl = (CompanyReport)Page.LoadControl("~/Controls/CompanyReport.ascx");
                reportControl.UserId = user.UserId;
                ph_report.Controls.Add(reportControl);
                report = reportControl;
            }
            else
            {
                var reportControl = (AgentReport)Page.LoadControl("~/Controls/AgentReport.ascx");
                reportControl.User = user;
                ph_report.Controls.Add(reportControl);
                report = reportControl;
            }
            lv_reportSearch.SearchUser(user);
            if (!IsPostBack)
                report.BindReport(lv_reportSearch.FromDate, lv_reportSearch.ToDate, user.UserId);
            InitEvents();
        }
        void InitEvents()
        {
            lv_reportSearch.OnSearchClick += (sender, e) =>
            {
                report.BindReport(lv_reportSearch.FromDate, lv_reportSearch.ToDate, user.UserId);
            };
            lv_reportSearch.OnReturnClick += (sender, e) =>
            {
                Response.Redirect(string.Format("~/WinLostReport.aspx?UserId={0}&From={1}&To={2}", user.ParentId,
                    lv_reportSearch.FromDate.ToString("yyyy-MM-dd"), lv_reportSearch.ToDate.ToString("yyyy-MM-dd")));
            };
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                report.BindReport(lv_reportSearch.FromDate, lv_reportSearch.ToDate, user.UserId);
            });
        }
    }
}